#!/bin/bash

set -e
#Check param
if [ $# -lt 1 ]
then
    echo "Usage: $0 {Platform} "
    echo "Platform:Linux/LinuxEmbedded/Windows32/Windows64/MacOS/iOS/Android"
    exit 1
fi

# version & tarfile & path 
OPENSSLVERSION=1.1.0c
DEPPATH=$(cd `dirname $0`; pwd)
TARBASENAME=OpenSSL_${OPENSSLVERSION//./_}
TAR=$TARBASENAME.tar.gz
OPENSSLDIR=$DEPPATH/openssl-$TARBASENAME

# check tar avaliable
CURL_OPTIONS=""                                                           #
if [ ! -e $TAR ]; then
  echo "Downloading $TAR"
  curl ${CURL_OPTIONS} -L -O https://github.com/openssl/openssl/archive/$TAR
else
  echo "Using $TAR"
fi

#set args
PLATFORM=$1
CC=cc
FLAGS=no-shared
OPENSSLOUT=`pwd`/openssl-$PLATFORM
PREFIX=--prefix=$OPENSSLOUT
CONFIG=

spinner()
{
  local pid=$!
  local delay=0.75
  local spinstr='|/-\'
  while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do
    local temp=${spinstr#?}
    printf " [%c]  " "$spinstr"
    local spinstr=$temp${spinstr%"$temp"}
    sleep $delay
    printf "\b\b\b\b\b\b"
  done
  printf "    \b\b\b\b"
}

build_Android()
{
    rm -fr $OPENSSLDIR
    mkdir -p $OPENSSLDIR
    cd $OPENSSLDIR

    NDKVERSION=r12                                  
    NDKDIR=/Users/max/Library/Android/android-ndk-$NDKVERSION

    #Build armeabi
    echo "Build openssl for armeabi..."
    echo "  tar -xzvf $TAR"
    rm -fr *
    tar -xzvf ../$TAR -C ../ >/dev/null 2>&1

    APIVERSION=16                                   #修改项
    ARCH=arch-arm                                   #修改项
    SUBPLATFORM=armeabi                             #修改项
    TOOLCHAIN=arm-linux-androideabi-4.9             #修改项
    TOOLNAME=arm-linux-androideabi                  #修改项
    INSTALLDIR=`pwd`/android-toolchain-armeabi      #修改项
    CONFIGPLATFORM=android                          #修改项
    PREFIX=--prefix=$OPENSSLOUT/$SUBPLATFORM
    LOGDIR=$OPENSSLOUT/$SUBPLATFORM
    LOGFILE=$LOGDIR/openssl-build-$SUBPLATFORM.log
    mkdir -p $LOGDIR

    export ANDROID_DEV=$NDKDIR/platforms/android-$APIVERSION/$ARCH/usr
    export NDK=$NDKDIR
    $NDK/build/tools/make-standalone-toolchain.sh --platform=android-$APIVERSION --toolchain=$TOOLCHAIN --install-dir=$INSTALLDIR >$LOGFILE 2>&1
    export TOOLCHAIN_PATH=`pwd`/android-toolchain-armeabi/bin
    export TOOL=$TOOLNAME
    export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
    export CC=$NDK_TOOLCHAIN_BASENAME-gcc
    export CXX=$NDK_TOOLCHAIN_BASENAME-g++
    export LINK=${CXX}
    export LD=$NDK_TOOLCHAIN_BASENAME-ld
    export AR=$NDK_TOOLCHAIN_BASENAME-ar
    export RANLIB=$NDK_TOOLCHAIN_BASENAME-ranlib
    export STRIP=$NDK_TOOLCHAIN_BASENAME-strip
    export ARCH_FLAGS="-mthumb"                     #修改项
    export ARCH_LINK=                               #修改项
    export CROSS_SYSROOT=$NDKDIR/platforms/android-$APIVERSION/$ARCH
    export CROSS_COMPILE=""
    export CPPFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
    export CXXFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 -frtti -fexceptions "
    export CFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
    export LDFLAGS=" ${ARCH_LINK} "

    echo "  Configure..."
    ./Configure $CONFIGPLATFORM $PREFIX $FLAGS >> $LOGFILE 2>&1
    echo "  Make..."
    PATH=$TOOLCHAIN_PATH:$PATH make >> $LOGFILE 2>&1 & spinner
    echo "  Make install..."
    make install_sw >> $LOGFILE 2>&1


    #Build armv7a
    echo "Build openssl for armv7a..."
    echo "  tar -xzvf $TAR"
    rm -fr *
    tar -xzvf ../$TAR -C ../ >/dev/null 2>&1
    
    APIVERSION=16                                   #修改项
    ARCH=arch-arm                                   #修改项
    SUBPLATFORM=armeabi-v7a                         #修改项
    TOOLCHAIN=arm-linux-androideabi-4.9             #修改项
    TOOLNAME=arm-linux-androideabi                  #修改项
    INSTALLDIR=`pwd`/android-toolchain-armv7a       #修改项
    CONFIGPLATFORM=android-armeabi                  #修改项
    PREFIX=--prefix=$OPENSSLOUT/$SUBPLATFORM        
    LOGDIR=$OPENSSLOUT/$SUBPLATFORM
    LOGFILE=$LOGDIR/openssl-build-$SUBPLATFORM.log
    mkdir -p $LOGDIR

    export ANDROID_DEV=$NDKDIR/platforms/android-$APIVERSION/$ARCH/usr
    export NDK=$NDKDIR
    $NDK/build/tools/make-standalone-toolchain.sh --platform=android-$APIVERSION --toolchain=$TOOLCHAIN --install-dir=$INSTALLDIR >$LOGFILE 2>&1
    export TOOLCHAIN_PATH=`pwd`/android-toolchain-armv7a/bin
    export TOOL=$TOOLNAME
    export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
    export CC=$NDK_TOOLCHAIN_BASENAME-gcc
    export CXX=$NDK_TOOLCHAIN_BASENAME-g++
    export LINK=${CXX}
    export LD=$NDK_TOOLCHAIN_BASENAME-ld
    export AR=$NDK_TOOLCHAIN_BASENAME-ar
    export RANLIB=$NDK_TOOLCHAIN_BASENAME-ranlib
    export STRIP=$NDK_TOOLCHAIN_BASENAME-strip
    export ARCH_FLAGS="-march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16"               #修改项
    export ARCH_LINK="-march=armv7-a -Wl,--fix-cortex-a8"                               #修改项
    export CROSS_SYSROOT=$NDKDIR/platforms/android-$APIVERSION/$ARCH
    export CROSS_COMPILE=""
    export CPPFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
    export CXXFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 -frtti -fexceptions "
    export CFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
    export LDFLAGS=" ${ARCH_LINK} "
    echo "  Configure..."
    ./Configure $CONFIGPLATFORM $PREFIX $FLAGS >>$LOGFILE 2>&1
    echo "  Make..."
    PATH=$TOOLCHAIN_PATH:$PATH make  >> $LOGFILE 2>&1 & spinner
    echo "  Make install..."
    make install_sw >>$LOGFILE 2>&1

    #Build arm64-v8a
    echo "Build openssl for arm64-v8a..."
    echo "  tar -xzvf $TAR"
    rm -fr *
    tar -xzvf ../$TAR -C ../ >/dev/null 2>&1

    APIVERSION=21                                   #修改项
    ARCH=arch-arm64                                 #修改项
    SUBPLATFORM=arm64-v8a                           #修改项
    TOOLCHAIN=aarch64-linux-android-4.9             #修改项
    TOOLNAME=aarch64-linux-android                  #修改项
    INSTALLDIR=`pwd`/android-toolchain-arm64-v8a    #修改项
    CONFIGPLATFORM=linux-generic64                  #修改项
    PREFIX=--prefix=$OPENSSLOUT/$SUBPLATFORM 
    LOGDIR=$OPENSSLOUT/$SUBPLATFORM
    LOGFILE=$LOGDIR/openssl-build-$SUBPLATFORM.log
    mkdir -p $LOGDIR

    export NDK=$NDKDIR
    export ANDROID_DEV=$NDKDIR/platforms/android-$APIVERSION/$ARCH/usr
    $NDK/build/tools/make-standalone-toolchain.sh --platform=android-$APIVERSION --toolchain=$TOOLCHAIN --install-dir=$INSTALLDIR > $LOGFILE 2>&1 
    export TOOLCHAIN_PATH=`pwd`/android-toolchain-arm64-v8a/bin
    export TOOL=$TOOLNAME
    export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
    export CC=$NDK_TOOLCHAIN_BASENAME-gcc
    export CXX=$NDK_TOOLCHAIN_BASENAME-g++
    export LINK=${CXX}
    export LD=$NDK_TOOLCHAIN_BASENAME-ld
    export AR=$NDK_TOOLCHAIN_BASENAME-ar
    export RANLIB=$NDK_TOOLCHAIN_BASENAME-ranlib
    export STRIP=$NDK_TOOLCHAIN_BASENAME-strip
    export ARCH_FLAGS=                              #修改项
    export ARCH_LINK=                               #修改项
    export CROSS_SYSROOT=$NDKDIR/platforms/android-$APIVERSION/$ARCH
    export CROSS_COMPILE=""
    export CPPFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
    export CXXFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 -frtti -fexceptions "
    export CFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
    export LDFLAGS=" ${ARCH_LINK} "
    echo "  Configure..."
    ./Configure $CONFIGPLATFORM $PREFIX $FLAGS  >> $LOGFILE 2>&1 
    echo "  Make..."
    PATH=$TOOLCHAIN_PATH:$PATH make   >> $LOGFILE 2>&1 & spinner
    echo "  Make install..."
    make install_sw >>$LOGFILE 2>&1

    #Build x86_32
    echo "Build openssl for x86_32..."
    echo "  tar -xzvf $TAR"
    rm -fr *
    tar -xzvf ../$TAR -C ../ >/dev/null 2>&1

    APIVERSION=16
    ARCH=arch-x86                                   #修改项
    SUBPLATFORM=x86                                 #修改项
    TOOLCHAIN=x86-4.9                               #修改项
    TOOLNAME=i686-linux-android                     #修改项
    INSTALLDIR=`pwd`/android-toolchain-x86          #修改项
    CONFIGPLATFORM=android-x86                      #修改项
    PREFIX=--prefix=$OPENSSLOUT/$SUBPLATFORM
    LOGDIR=$OPENSSLOUT/$SUBPLATFORM
    LOGFILE=$LOGDIR/openssl-build-$SUBPLATFORM.log
    mkdir -p $LOGDIR
               
    export NDK=$NDKDIR
    export ANDROID_DEV=$NDKDIR/platforms/android-$APIVERSION/$ARCH/usr
    $NDK/build/tools/make-standalone-toolchain.sh --platform=android-$APIVERSION --toolchain=$TOOLCHAIN --install-dir=$INSTALLDIR > $LOGFILE 2>&1 
    export TOOLCHAIN_PATH=`pwd`/android-toolchain-x86/bin
    export TOOL=$TOOLNAME
    export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
    export CC=$NDK_TOOLCHAIN_BASENAME-gcc
    export CXX=$NDK_TOOLCHAIN_BASENAME-g++
    export LINK=${CXX}
    export LD=$NDK_TOOLCHAIN_BASENAME-ld
    export AR=$NDK_TOOLCHAIN_BASENAME-ar
    export RANLIB=$NDK_TOOLCHAIN_BASENAME-ranlib
    export STRIP=$NDK_TOOLCHAIN_BASENAME-strip
    export ARCH_FLAGS="-march=i686 -msse3 -mstackrealign -mfpmath=sse"          #修改项
    export ARCH_LINK=                                                           #修改项
    export CROSS_SYSROOT=$NDKDIR/platforms/android-$APIVERSION/$ARCH
    export CROSS_COMPILE=""
    export CPPFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
    export CXXFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 -frtti -fexceptions "
    export CFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
    export LDFLAGS=" ${ARCH_LINK} "
    echo "  Configure..."
    ./Configure $CONFIGPLATFORM $PREFIX $FLAGS >> $LOGFILE 2>&1 
    echo "  Make..."
    PATH=$TOOLCHAIN_PATH:$PATH make  >> $LOGFILE 2>&1 &spinner
    echo "  Make install..."
    make install_sw >>$LOGFILE 2>&1

    #Build x86_64
    echo "Build openssl for x86_64..."
    echo "  tar -xzvf $TAR"
    rm -fr *
    tar -xzvf ../$TAR -C ../ >/dev/null 2>&1

    APIVERSION=21                                   #修改项
    ARCH=arch-x86_64                                #修改项
    SUBPLATFORM=x86_64                              #修改项
    TOOLCHAIN=x86_64-4.9                            #修改项
    TOOLNAME=x86_64-linux-android                   #修改项
    INSTALLDIR=`pwd`/android-toolchain-x86_64       #修改项
    CONFIGPLATFORM=linux-x86_64                     #修改项
    PREFIX=--prefix=$OPENSSLOUT/$SUBPLATFORM
    LOGDIR=$OPENSSLOUT/$SUBPLATFORM
    LOGFILE=$LOGDIR/openssl-build-$SUBPLATFORM.log
    mkdir -p $LOGDIR
               
    export NDK=$NDKDIR
    export ANDROID_DEV=$NDKDIR/platforms/android-$APIVERSION/$ARCH/usr
    $NDK/build/tools/make-standalone-toolchain.sh --platform=android-$APIVERSION --toolchain=$TOOLCHAIN --install-dir=$INSTALLDIR  > $LOGFILE 2>&1 
    export TOOLCHAIN_PATH=`pwd`/android-toolchain-x86_64/bin
    export TOOL=$TOOLNAME
    export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
    export CC=$NDK_TOOLCHAIN_BASENAME-gcc
    export CXX=$NDK_TOOLCHAIN_BASENAME-g++
    export LINK=${CXX}
    export LD=$NDK_TOOLCHAIN_BASENAME-ld
    export AR=$NDK_TOOLCHAIN_BASENAME-ar
    export RANLIB=$NDK_TOOLCHAIN_BASENAME-ranlib
    export STRIP=$NDK_TOOLCHAIN_BASENAME-strip
    export ARCH_FLAGS=                              #修改项
    export ARCH_LINK=                               #修改项
    export CROSS_SYSROOT=$NDKDIR/platforms/android-$APIVERSION/$ARCH
    export CROSS_COMPILE=""
    export CPPFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
    export CXXFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 -frtti -fexceptions "
    export CFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
    export LDFLAGS=" ${ARCH_LINK} "
    echo "  Configure..."
    ./Configure $CONFIGPLATFORM $PREFIX $FLAGS >> $LOGFILE 2>&1 
    echo "  Make..."
    PATH=$TOOLCHAIN_PATH:$PATH make  >> $LOGFILE 2>&1 &spinner
    echo "  Make install..."
    make install_sw >>$LOGFILE 2>&1

    #Build mips
    echo "Build openssl for mips_32..."
    echo "  tar -xzvf $TAR"
    rm -fr *
    tar -xzvf ../$TAR -C ../ >/dev/null 2>&1

    APIVERSION=16                                   #修改项
    ARCH=arch-mips                                  #修改项
    SUBPLATFORM=mips                                #修改项
    TOOLCHAIN=mipsel-linux-android-4.9              #修改项
    TOOLNAME=mipsel-linux-android                   #修改项
    INSTALLDIR=`pwd`/android-toolchain-mips         #修改项
    CONFIGPLATFORM="android-mips"                   #修改项
    PREFIX=--prefix=$OPENSSLOUT/$SUBPLATFORM 
    LOGDIR=$OPENSSLOUT/$SUBPLATFORM
    LOGFILE=$LOGDIR/openssl-build-$SUBPLATFORM.log
    mkdir -p $LOGDIR
              
    export NDK=$NDKDIR
    export ANDROID_DEV=$NDKDIR/platforms/android-$APIVERSION/$ARCH/usr
    $NDK/build/tools/make-standalone-toolchain.sh --platform=android-$APIVERSION --toolchain=$TOOLCHAIN --install-dir=$INSTALLDIR > $LOGFILE 2>&1 
    export TOOLCHAIN_PATH=`pwd`/android-toolchain-mips/bin
    export TOOL=$TOOLNAME
    export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
    export CC=$NDK_TOOLCHAIN_BASENAME-gcc
    export CXX=$NDK_TOOLCHAIN_BASENAME-g++
    export LINK=${CXX}
    export LD=$NDK_TOOLCHAIN_BASENAME-ld
    export AR=$NDK_TOOLCHAIN_BASENAME-ar
    export RANLIB=$NDK_TOOLCHAIN_BASENAME-ranlib
    export STRIP=$NDK_TOOLCHAIN_BASENAME-strip
    export ARCH_FLAGS=                              #修改项
    export ARCH_LINK=                               #修改项
    export CROSS_SYSROOT=$NDKDIR/platforms/android-$APIVERSION/$ARCH
    export CROSS_COMPILE=""
    export CPPFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
    export CXXFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 -frtti -fexceptions "
    export CFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
    export LDFLAGS=" ${ARCH_LINK} "
    echo "  Configure..."
    ./Configure $CONFIGPLATFORM $PREFIX $FLAGS >> $LOGFILE 2>&1 
    echo "  Make..."
    PATH=$TOOLCHAIN_PATH:$PATH make  >> $LOGFILE 2>&1  &spinner
    echo "  Make install..."
    make install_sw >>$LOGFILE 2>&1

    #Build mips64
    echo "Build openssl for mips_64..."
    echo "  tar -xzvf $TAR"
    rm -fr *
    tar -xzvf ../$TAR -C ../ >/dev/null 2>&1

    APIVERSION=21                                   #修改项
    ARCH=arch-mips64                                #修改项
    SUBPLATFORM=mips64                              #修改项
    TOOLCHAIN=mips64el-linux-android-4.9            #修改项
    TOOLNAME=mips64el-linux-android                 #修改项
    INSTALLDIR=`pwd`/android-toolchain-mips64       #修改项
    CONFIGPLATFORM="linux-generic64"                #修改项
    PREFIX=--prefix=$OPENSSLOUT/$SUBPLATFORM
    LOGDIR=$OPENSSLOUT/$SUBPLATFORM
    LOGFILE=$LOGDIR/openssl-build-$SUBPLATFORM.log
    mkdir -p $LOGDIR     

    export NDK=$NDKDIR
    export ANDROID_DEV=$NDKDIR/platforms/android-$APIVERSION/$ARCH/usr
    $NDK/build/tools/make-standalone-toolchain.sh --platform=android-$APIVERSION --toolchain=$TOOLCHAIN --install-dir=$INSTALLDIR > $LOGFILE 2>&1 
    export TOOLCHAIN_PATH=`pwd`/android-toolchain-mips64/bin
    export TOOL=$TOOLNAME
    export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
    export CC=$NDK_TOOLCHAIN_BASENAME-gcc
    export CXX=$NDK_TOOLCHAIN_BASENAME-g++
    export LINK=${CXX}
    export LD=$NDK_TOOLCHAIN_BASENAME-ld
    export AR=$NDK_TOOLCHAIN_BASENAME-ar
    export RANLIB=$NDK_TOOLCHAIN_BASENAME-ranlib
    export STRIP=$NDK_TOOLCHAIN_BASENAME-strip
    export ARCH_FLAGS=                              #修改项
    export ARCH_LINK=                               #修改项
    export CROSS_SYSROOT=$NDKDIR/platforms/android-$APIVERSION/$ARCH
    export CROSS_COMPILE=""
    export CPPFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
    export CXXFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 -frtti -fexceptions "
    export CFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
    export LDFLAGS=" ${ARCH_LINK} "
    echo "  Configure..."
    ./Configure $CONFIGPLATFORM $PREFIX $FLAGS >> $LOGFILE 2>&1 
    echo "  Make..."
    PATH=$TOOLCHAIN_PATH:$PATH make  >> $LOGFILE 2>&1 &spinner
    echo "  Make install..."
    make install_sw >>$LOGFILE 2>&1
}
build_Linux()
{
    rm -fr $OPENSSLDIR
    tar -xzvf $TAR
    cd $OPENSSLDIR

    CONFIG="./config $PREFIX $FLAGS"
    $CONFIG

    #Compile
    make depend
    make 
    make install_sw
}

build_Windows32()
{
    #Windows在mingw+msys的环境下编译，需要提前准备环境
    rm -fr $OPENSSLDIR
    tar -xzvf $TAR
    cd $OPENSSLDIR
    
    ./Configure mingw $PREFIX $FLAGS
    
    #Compile
    make 
    make install_sw
}

build_Windows64()
{
    #Windows在mingw+msys的环境下编译，需要提前准备环境
    rm -fr $OPENSSLDIR
    tar -xzvf $TAR
    cd $OPENSSLDIR

    ./Configure mingw64 $PREFIX $FLAGS

    #Compile
    make 
    make install_sw
}

build_Linux-Embedded()
{
    #Check toolchian
    {
        arm-hisiv100nptl-linux-gcc --version 2>>/dev/null
        if [ $? -ne 0 ]
        then
            echo "$CC(mingw) can not found, please install"
        fi
    }

    FLAGS="no-asm $FLAGS"

    rm -fr $OPENSSLDIR
    tar -xzvf $TAR
    cd $OPENSSLDIR

    CONFIG="./config $PREFIX $FLAGS"
    CC=arm-hisiv100nptl-linux-gcc $CONFIG

    #Compile
    make 
    make install_sw
}

build_MacOS()
{
    rm -fr $OPENSSLDIR
    tar -xzvf $TAR
    cd $OPENSSLDIR

    CONFIG="./Configure darwin64-x86_64-cc $PREFIX $FLAGS"
    $CONFIG

    #Compile
    make depend
    make 
    make install_sw
}

build_iOS()
{
    OPENSSLDIR=OpenSSL-for-iPhone
    cd $OPENSSLDIR

    ./build-libssl.sh --version=$OPENSSLVERSION

    cd ..
    OPENSSLOUT=openssl-$PLATFORM
    rm -fr  $OPENSSLOUT
    mkdir $OPENSSLOUT
    cp -fr $OPENSSLDIR/lib $OPENSSLDIR/include $OPENSSLOUT
}


if [ "$PLATFORM" == "Linux" ]
then
    build_Linux
elif [ "$PLATFORM" == "LinuxEmbedded" ]
then
    build_Linux-Embedded
elif [ "$PLATFORM" == "Windows32" ]
then
    build_Windows32
elif [ "$PLATFORM" == "Windows64" ]
then
    build_Windows64
elif [ "$PLATFORM" == "MacOS" ]
then
    build_MacOS
elif [ "$PLATFORM" == "iOS" ]
then
    build_iOS
elif [ "$PLATFORM" == "Android" ]
then
    build_Android
else
    echo "unrecognized platform : $PLATFORM"
    echo "Platform:Linux/LinuxEmbedded/Windows32/Windows64/MacOS/iOS/Android"
    exit 1
fi

